package com.linlong.linlongchat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linlong.linlongchat.domain.po.Apply;
import com.linlong.linlongchat.domain.vo.ApplyVo;
import com.linlong.linlongchat.mapper.ApplyMapper;
import com.linlong.linlongchat.service.IApplyService;
import com.linlong.linlongchat.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 申请表 服务实现类
 * </p>
 *
 * @author 华少
 * @since 2024-09-19
 */
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements IApplyService {


    @Autowired
    private ApplyMapper applyMapper;

    @Override
    public boolean addApply(String responseAccount) {
        Apply apply = new Apply();
        apply.setStatus(0);
        apply.setCreateTime(LocalDateTime.now());
        apply.setUpdateTime(LocalDateTime.now());
        Map<String, Object> claims = ThreadLocalUtil.get();
        apply.setRequestAccount((String) claims.get("account"));
        apply.setResponseAccount(responseAccount);
        if(applyMapper.selectByAccount(apply.getRequestAccount(),apply.getResponseAccount())!=null){
            return false;
        }else{
            return applyMapper.insert(apply) > 0;
        }

    }

    @Override
    public boolean updateApplyStatus(Long id, Integer status) {
        Apply apply = applyMapper.selectById(id);
        apply.setStatus(status);
        apply.setUpdateTime(LocalDateTime.now());
        return applyMapper.updateById(apply) > 0;
    }

    @Override
    public List<ApplyVo> getAllApplies() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String account = (String) claims.get("account");
        // 使用 MyBatis-Plus 的条件构造器进行排序查询
        QueryWrapper<Apply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("response_account", account).eq("status", 0).orderByDesc("update_time");
        List<Apply> applies = applyMapper.selectList(queryWrapper);
        QueryWrapper<Apply> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("response_account", account).ne("status", 0).orderByDesc("update_time");
        applies.addAll(applyMapper.selectList(queryWrapper1));
        return applies.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public Long CountAllNotCheckedStatus() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String account = (String) claims.get("account");
        QueryWrapper<Apply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("response_account", account).eq("status", 0);
        return applyMapper.selectCount(queryWrapper);
    }

    private ApplyVo convertToVO(Apply apply) {
        ApplyVo applyVo = new ApplyVo();
        BeanUtils.copyProperties(apply, applyVo);
        // 根据需要进行时间格式转换等操作
        return applyVo;
    }

}
